import { ref, reactive } from 'vue';
import { addRecipient, queryRecipients, addOneRecipient,deleteRecipient } from '@/axios/api/CertificateCode';
import { ElNotification } from 'element-plus';
import { useCounterStore } from '@/stores/counter';
const counterStore = useCounterStore();
export default function () {
    // 当前页数
    let currentPage4 = ref(1);
    // 每页显示条数
    let pageSize4 = ref(10);
    // 总条数
    let total4 = ref(0);

    // 国家代码
    let Item = ref('')

    // 国家名称
    let CertificateCode = ref('')

    let recipientsList = ref<any>([]);

    // 每页显示条目个数改变时触发
    const handleSizeChange = () => {
        currentPage4.value = 1;
        queryRecipientsList();
    };

    // 当前页数改变时触发
    const handleCurrentChange = () => {
        queryRecipientsList();
    };
    async function importRecipients() {
        try {
            counterStore.flag = true;
            const res: any = await addRecipient();
            queryRecipientsList();
            showMessage(res.code, res.message);
            counterStore.flag = false;
        } catch (err: any) {
            counterStore.flag = false;
            showMessage(err.code, err.message);
        }
      };
    // 查询联系人
    async function queryRecipientsList() {
        try {
            let res: any = await queryRecipients(
                Item.value,
                CertificateCode.value,
                currentPage4.value,
                pageSize4.value
            );
            if (res.code == 200) {
                recipientsList.value = res.data;
                total4.value = res.total;
            } else {
                recipientsList.value = [];
                total4.value = 0;
                showMessage(res.code, res.message);
            }

        } catch (err: any) {
            recipientsList.value = [];
            showMessage(err.code, err.message);
        }
    }
    // 显示通知消息
    function showMessage(type: number, message: string) {
        const title = type === 200 ? 'Success' : 'Error';
        ElNotification({
            title: title,
            message: message,
            type: type === 200 ? 'success' : 'error',
        });
    }


    // 抽屉
    let dialog = ref(false); // 是否显示抽屉
    let recipientsList_data = reactive({
        id: '',
        Item: '',
        CertificateCode: ''
    })
    function cancel() {
        dialog.value = false;
    }
    // 添加或编辑收件人
    const handleAdd = (id: string,Item: string,CertificateCode: string) => {
        recipientsList_data.id = '';
        recipientsList_data.Item = '';
        recipientsList_data.CertificateCode = '';
        recipientsList_data.id = id;
        recipientsList_data.Item=Item;
        recipientsList_data.CertificateCode = CertificateCode;
        dialog.value = true;
    }

    // 添加收件人提交
    const handleAddsubmit = async (formrecipients: any) => {
        await formrecipients.validate();
        try {
            let res: any = await addOneRecipient(recipientsList_data);
            ElNotification({
                title: 'Success',
                message: res.msg,
                type: 'success',
            });
            dialog.value = false;
            currentPage4.value = 1;
            queryRecipientsList();
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
        }
    }

    // 抽屉表单校验
    const Item1 = (_rule: any, value: any, callback: any) => {
        if (value === '') {
            callback(new Error('请输入Item'))
        } else {
            callback() //通过
        }
    }
    const CertificateCode1 = (_rule: any, value: any, callback: any) => {
       if (value === '') {
            callback(new Error('请输入CertificateCode'))
        } else {
            callback() //通过
        }
    }

    const rules = {
        Item: [
            { validator: Item1, trigger: 'change' }
        ],
        CertificateCode: [
            { validator: CertificateCode1, trigger: 'change' }
        ]

    }

    // 删除收件人
    const handleDelete = async (id: string) => {
        try {
            let res: any = await deleteRecipient(id);
            ElNotification({
                title: 'Success',
                message: res.msg,
                type:'success',
            });
            currentPage4.value = 1;
            queryRecipientsList();
        } catch (error: any) {
            ElNotification({
                title: 'Error',
                message: error,
                type: 'error',
            })
        }
    }
    return {
        currentPage4,
        pageSize4,
        total4,
        Item,
        CertificateCode,
        recipientsList,
        dialog,
        recipientsList_data,
        rules,
        cancel,
        handleSizeChange,
        handleCurrentChange,
        importRecipients,
        queryRecipientsList,
        handleAdd,
        handleAddsubmit,
        handleDelete
    }
};